import socket
import time
import traceback

from py.uitls import DeviceUtil
from py.uitls.Logger import myLogger


def device_info():
    try:
        pc_name = socket.gethostname()
        current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        start_info = "<p>电脑名称：%s</p><p>开机时间：%s</p>" % (pc_name, current_time)
    except Exception as e:
        start_info = "<p>开机错误信息：</p>" + traceback.format_exc()
        myLogger.exception(e)

    try:
        devices = DeviceUtil.device_info()
        device_info = '<br/>'.join(devices)
    except Exception as e:
        device_info = "<p>设备错误信息：</p>" + traceback.format_exc()
        myLogger.exception(e)

    try:
        pic_file = DeviceUtil.screen()
        pic_error = '截屏成功，请查看附件'
    except Exception as e:
        pic_file = None
        pic_error = "<p>截屏错误信息：</p>" + traceback.format_exc()
        myLogger.exception(e)

    try:
        ip_obj = DeviceUtil.ip_info()
        ip_info = '<p>IP地址：%s</p><p>地理位置：%s</p>' % (ip_obj['cip'], ip_obj['cname'])
    except Exception as e:
        ip_info = "<p>IP错误信息：</p>" + traceback.format_exc()
        myLogger.exception(e)

    message = start_info + '<hr/>' + ip_info + '<hr/>' + device_info + '<hr/>' + pic_error
    return message, pic_file
